package br.org.ifrr.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.org.ifrr.model.Gestor;
import br.org.ifrr.repository.Gestores;
import br.org.ifrr.util.cdi.CDIServiceLocator;

@FacesConverter(forClass = Gestor.class)
public class GestorConverter implements Converter {

	// @Inject
	private Gestores gestores;

	public GestorConverter() {
		gestores = CDIServiceLocator.getBean(Gestores.class);
	}

	@Override
	public Object getAsObject(FacesContext context, UIComponent componente,
			String value) {
		Gestor retorno = null;

		if (value != null) {
			Long id = new Long(value);
			retorno = gestores.porId(id);
		}
		return retorno;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {

		if (value != null) {
			Gestor gestor = (Gestor) value;
			return gestor.getId() == null ? null : gestor.getId().toString();
		}
		return "";
	}
}
